<?php

namespace App\Http\Controllers\H5\Shop;

use App\Common\Exceptions\AixinException;
use App\Common\Exceptions\AixinExceptionCode;
use App\Common\Model\ShopPayUnline;
use App\Common\Service\ShopAccountService;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class PeakReportController extends Controller
{
    //控制器操作service>model>data
    protected $shopAccountService;
    protected $shopPayUnline;
    public function __construct(ShopAccountService $shopAccountService,ShopPayUnline $shopPayUnline)
    {
        $this->shopAccountService = $shopAccountService;
        $this->shopPayUnline = $shopPayUnline;
    }
    
    //用户复投提交请求的接口
    public function double(Request $request)
    {
        //uid  name  num types为1 status为0
        $num =$request->num;
        $uid = $request->uid;
        $name = $request->name;
        $types = $request->types;
        //判段参数
        if(empty($uid) || empty($num) || empty($name) || empty($types)){
            throw new AixinException(AixinExceptionCode::MISSING_PARAM);
        }
        //根据用户的id去查询是否有正在审核状态中的记录
        $douStatus = $this->shopPayUnline->getDouStatus($uid,$types,$status=0);
        if(!empty($douStatus)){
            $res = [
                'recode'=> 1001
            ];
            $msg = "对不起，您已有单据正在审核!";
            return ApiResponse($res, $msg);
        }
        //判段用户是否重复提交的单号
        $douNum = $this->shopPayUnline->getDouNum($uid,$types,$num);
        if(!empty($douNum)){
            $res = [
                'recode'=> 1002
            ];
            $msg = "对不起，该单据已有审核记录!请勿重复提交!";
            return ApiResponse($res, $msg);
        }
        //判段全表是否有重复的num
        $numLnfo = $this->shopPayUnline->getNumInfo($num);
        if(!empty($numLnfo)){
            $res = [
                'recode'=> 1003
            ];
            $msg = "对不起，该单据已有审核记录!";
            return ApiResponse($res, $msg);
        }

        //数据写入
        $data = [
            'uid'=>$uid,
            'num'=>$num,
            'name'=>$name,
            'types'=>$types, //前端传过来的应为:1
            'create_time'=>time(),
            'update_time'=>time(),
            'status'=>0     //状态为未审核为:0;
        ];
        $result=$this->shopPayUnline->addDoubleRecord($data);
        if($result){
            return ApiResponse([]);
        }else{
            return ApiResponse([],'复投提交失败',1);
        }

    }  
    
    //用户的复投后的刷新请求页面
    public function douRefresh(Request $request)
    {
        if($request->has('uid')){
            //根据用户ID去调用shop_pay_unline的model查询用户的审核情况
            $result = $this->shopPayUnline->getDouStatus($request->uid,1,0);
            if(!empty($result)){
                //有正在审核的
                return ApiResponse(['recode'=>1],'财务未通过审核!');
            }else{
                //没有正在审核的
                return ApiResponse(['recode'=>0],'已通过复投审核!');
            }
        }else{
            throw new AixinException(AixinExceptionCode::MISSING_PARAM);
        }
    }

}
